using Avalonia.Controls;
using Avalonia.Interactivity;
using DesktopApp.ViewModels;

namespace DesktopApp.Views
{
    public partial class RuleEditorWindow : Window
    {
        public RuleEditorWindow()
        {
            InitializeComponent();
        }

        // Property to hold the result (the new/edited rule)
        public RuleItemViewModel? ResultRule { get; private set; }

        private void SaveButton_Click(object? sender, RoutedEventArgs e)
        {
            if (DataContext is RuleEditorViewModel vm)
            {
                ResultRule = vm.GetRuleItemViewModel();
                if (ResultRule != null) // Only close if validation passed (GetRuleItemViewModel didn't return null)
                {
                    Close(ResultRule); // Close the window and pass the result
                }
                else
                {
                    // Optionally show validation error message here
                    System.Diagnostics.Debug.WriteLine("Save aborted due to validation errors.");
                }
            }
            else
            {
                 Close(null); // Close without result if DataContext is wrong (should not happen ideally)
            }
        }

        private void CancelButton_Click(object? sender, RoutedEventArgs e)
        {
            Close(null); // Close the window without returning a result
        }
    }
} 